//
//  IMTypeViewController.m
//  cliqset
//
//  Created by David Hinson on 2/18/09.
//  Copyright 2009 Sumner Systems Management, Inc.. All rights reserved.
//

#import "IMTypeViewController.h"
#import "cliqsetAppDelegate.h"

@implementation IMTypeViewController

@synthesize imTypes;
@synthesize parent;
@synthesize lastSelectedRow;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.title = @"Label";
	
	self.imTypes.backgroundColor = [UIColor clearColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

// Number of table sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}

// Return rows in table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return 8;
}	

// Draw cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
	UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
    [cell backgroundView].opaque=YES;
    [cell backgroundView].clearsContextBeforeDrawing;
   	[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
	
	UIFont* bold_large_font = [UIFont boldSystemFontOfSize:18];
	UIColor* textColor      = [UIColor colorWithRed:85.0/255.0 green:85.0/255.0 blue:85.0/255.0 alpha:1.0];
	
	cell.hidesAccessoryWhenEditing = NO;
	
	UILabel *label  = [[UILabel alloc] initWithFrame:CGRectMake(20, 12, 200, 20)];
	label.textColor = textColor;
	label.font      = bold_large_font;

	switch (indexPath.row) {
		case 0:
			label.text = @"aim";
			if ([self.parent.imTypeField isEqualToString:@"aim"]) {
				cell.accessoryType = UITableViewCellAccessoryCheckmark;
				self.lastSelectedRow = indexPath.row;
			}
			break;
		case 1:
			label.text = @"gtalk";
			if ([self.parent.imTypeField isEqualToString:@"gtalk"]) {
				cell.accessoryType = UITableViewCellAccessoryCheckmark;
				self.lastSelectedRow = indexPath.row;
			}
			break;
		case 2:
			label.text = @"icq";
			if ([self.parent.imTypeField isEqualToString:@"icq"]) {
				cell.accessoryType = UITableViewCellAccessoryCheckmark;
				self.lastSelectedRow = indexPath.row;
			}
			break;
		case 3:
			label.text = @"xmpp";
			if ([self.parent.imTypeField isEqualToString:@"xmpp"]) {
				cell.accessoryType = UITableViewCellAccessoryCheckmark;
				self.lastSelectedRow = indexPath.row;
			}
			break;
		case 4:
			label.text = @"skype";
			if ([self.parent.imTypeField isEqualToString:@"skype"]) {
				cell.accessoryType = UITableViewCellAccessoryCheckmark;
				self.lastSelectedRow = indexPath.row;
			}
			break;
		case 5:
			label.text = @"qq";
			if ([self.parent.imTypeField isEqualToString:@"qq"]) {
				cell.accessoryType = UITableViewCellAccessoryCheckmark;
				self.lastSelectedRow = indexPath.row;
			}
			break;
		case 6:
			label.text = @"yahoo";
			if ([self.parent.imTypeField isEqualToString:@"yahoo"]) {
				cell.accessoryType = UITableViewCellAccessoryCheckmark;
				self.lastSelectedRow = indexPath.row;
			}
			break;
		case 7:
			label.text = @"msn";
			if ([self.parent.imTypeField isEqualToString:@"msn"]) {
				cell.accessoryType = UITableViewCellAccessoryCheckmark;
				self.lastSelectedRow = indexPath.row;
			}
			break;
	}
	
	[cell addSubview:label];
	[label release];

	UIColor* myBGColor               = [UIColor colorWithRed:248.0/255.0 green:248.0/255.0 blue:248.0/255.0 alpha:1.0];
	cell.backgroundView.backgroundColor = myBGColor;
	for ( UIView* view in cell.contentView.subviews ) 
	{
		view.backgroundColor = [ UIColor clearColor ];
	}
	
	return cell;
}

// Indicate disclosure
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
	if (self.lastSelectedRow == indexPath.row) {
		return UITableViewCellAccessoryCheckmark;	
	} 
	else {
		return UITableViewCellAccessoryNone;
	}
}

// Row selected... 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {	
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];
	
	NSLog(@"didSelectRowAtIndexPath");
	
	lastSelectedRow = indexPath.row;
	
	UITableViewCell *hitCell = [tableView cellForRowAtIndexPath:indexPath];	
    [hitCell setSelected:NO animated:YES];
	
	if (indexPath.section == 0) {
		
		switch (indexPath.row) {
			case 0:
				self.parent.imTypeField = @"aim";
				break;
			case 1:
				self.parent.imTypeField = @"gtalk";
				break;
			case 2:
				self.parent.imTypeField = @"icq";
				break;
			case 3:
				self.parent.imTypeField = @"xmpp";
				break;
			case 4:
				self.parent.imTypeField = @"skype";
				break;
			case 5:
				self.parent.imTypeField = @"qq";
				break;
			case 6:
				self.parent.imTypeField = @"yahoo";
				break;
			case 7:
				self.parent.imTypeField = @"msn";
				break;
		}
		
		[self.imTypes reloadData];
		
		[myApp slideIMTypeOut:parent];
	}
}

- (void)dealloc {
	[imTypes release];
	[parent release];
    [super dealloc];
}


@end
